package cn.edu.controller.system;

import cn.edu.service.DoctorService;
import cn.edu.service.RoleService;
import cn.edu.bean.Doctor;
import cn.edu.bean.RespBean;
import cn.edu.bean.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by sang on 2018/1/2.
 */
@RestController
@RequestMapping("/system/doctor")
public class SystemDoctorController {
    @Autowired
    DoctorService doctorService;
    @Autowired
    RoleService roleService;

    @RequestMapping("/id/{doctorId}")
    public Doctor getDoctorById(@PathVariable Long doctorId) {
        return doctorService.getDoctorById(doctorId);
    }

    @RequestMapping(value = "/{doctorId}", method = RequestMethod.DELETE)
    public RespBean deleteDoctor(@PathVariable Long doctorId) {
        if (doctorService.deleteDoctor(doctorId) == 1) {
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败!");
    }

    @RequestMapping(value = "/", method = RequestMethod.PUT)
    public RespBean updateDoctor(Doctor doctor) {
        if (doctorService.updateDoctor(doctor) == 1) {
            return RespBean.ok("更新成功!");
        }
        return RespBean.error("更新失败!");
    }

    @RequestMapping(value = "/roles", method = RequestMethod.PUT)
    public RespBean updateDoctorRoles(Long doctorId, Long[] rids) {
        if (doctorService.updateDoctorRoles(doctorId, rids) == rids.length) {
            return RespBean.ok("更新成功!");
        }
        return RespBean.error("更新失败!");
    }

    @RequestMapping("/{keywords}")
    public List<Doctor> getDoctorsByKeywords(@PathVariable(required = false) String keywords) {
        List<Doctor> doctors = doctorService.getDoctorsByKeywords(keywords);
        return doctors;
    }

    @RequestMapping(value = "/roles",method = RequestMethod.GET)
    public List<Role> allRoles() {
        return roleService.roles();
    }

    @RequestMapping(value = "/doctor/reg", method = RequestMethod.POST)
    public RespBean doctorReg(String username, String password) {
        int i = doctorService.doctorReg(username, password);
        if (i == 1) {
            return RespBean.ok("注册成功!");
        } else if (i == -1) {
            return RespBean.error("用户名重复，注册失败!");
        }
        return RespBean.error("注册失败!");
    }

}
